import React, {Component} from 'react';
import { Form, Icon, Input, Button, Checkbox, message } from 'antd';
const FormItem = Form.Item;
import sha1 from 'sha1';
import {post} from '../../common/request';
import styles from './loginPage.css';

class LoginPage extends Component {

    handleSubmit(e){
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
          if (!err) {

              post('/login',values).then((res) => {
                if (res.code === 1){
                  message.success('登录成功')
                  this.props.history.push('/admin');
                }else if (res.code === 0){
                  message.error(res.message);
                }
              }).catch((err)=>{
                message.error(err.message);
              });
          }
        });
      }
      render() {

        const { getFieldDecorator } = this.props.form;
        return (
          <Form onSubmit={this.handleSubmit.bind(this)} className={["login-form",styles.loginForm]} layout="vertical" >
            <FormItem>
              {getFieldDecorator('userName', {
                rules: [{ required: true, message: 'Please input your username!' }],
              })(
                <Input prefix={<Icon type="user" style={{ fontSize: 13 }} />} placeholder="Username" />
              )}
            </FormItem>
            <FormItem>
              {getFieldDecorator('password', {
                rules: [{ required: true, message: 'Please input your Password!' }],
              })(
                <Input prefix={<Icon type="lock" style={{ fontSize: 13 }} />} type="password" placeholder="Password" />
              )}
            </FormItem>
            <FormItem>
              <div>
                <Button type="primary" htmlType="submit" className={styles.loginFormButton}>
                    Log in
                </Button>
              </div>
              Or <a href="">register now!</a>
            </FormItem>
          </Form>
        );
      }
}

const WrappedNormalLoginForm = Form.create()(LoginPage);

export default WrappedNormalLoginForm;